package source;

import java.util.concurrent.atomic.AtomicInteger;

public class SupplyDepot {
	private AtomicInteger gold = new AtomicInteger(0);
	private AtomicInteger wood = new AtomicInteger(0);
	private AtomicInteger stone = new AtomicInteger(0);
	private AtomicInteger food = new AtomicInteger(0);

	private Object resourceLock = new Object();

	public void addGold(int quantity) {
		gold.addAndGet(quantity);
	}

	public void addStone(int quantity) {
		stone.addAndGet(quantity);
	}

	public void addWood(int quantity) {
		wood.addAndGet(quantity);
	}

	public void addFood(int quantity) {
		food.addAndGet(quantity);
	}

	public boolean getMaterialsForConstruction(int goldQuant, int woodQuant,
			int stoneQuant) {
		synchronized (resourceLock) {
			if ((stone.get() >= stoneQuant) && (wood.get() >= woodQuant)
					&& (gold.get() >= goldQuant)) {
				stone.addAndGet(-stoneQuant);
				gold.addAndGet(-stoneQuant);
				wood.addAndGet(-stoneQuant);
				return true;
			}

			return false;
		}
	}

	public boolean getMaterialsForDrone(int goldQuant, int foodQuant) {
		synchronized (resourceLock) {
			if ((gold.get() >= goldQuant) && (food.get() >= foodQuant)) {
				gold.addAndGet(-goldQuant);
				food.addAndGet(-foodQuant);
				return true;
			}
			return false;
		}
	}
}
